home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3852 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  50 lines

  1. Path: news.delphi.com!usenet
  2. From: Derek Harmon <stonelight@delphi.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: New C Programmer Has A Problem
  5. Date: Tue, 30 Jan 96 14:25:21 -0500
  6. Organization: Delphi (info@delphi.com email, 800-695-4005 voice)
  7. Message-ID: <JLNqQp5.stonelight@delphi.com>
  8. References: <4ehpa3$6kl@nntp.novia.net> <4ek12b$1li@clare.res.com>
  9. NNTP-Posting-Host: bos1f.delphi.com
  10. X-To: <danlynes@res.com>
  11.  
  12. ** Quoting a reply posted by <danlynes@res.com> dated <30-Jan-1996>:
  13.  
  14. > >In <4ehpa3$6kl@nntp.novia.net>, tsyslo@oasis.novia.net (Tony Syslo) writes:
  15. :
  16. > >    }
  17. > >    /* We have now opened a file, and are ready to start writing: */
  18. > >    bytes_written=Write(file_handle,name,sizeof(name));
  19. > >    bytes_written=bytes_written+Write(file_handle,age,sizeof(age));
  20. >
  21. > You cannot get sizeof( name ), as name is an uninitialize pointer.  Please
  22. > see my reference to malloc above.  As I used malloc, we will not use sizeof,
  23. > as you cannot get a sizeof of a generic pointer...only a struct, union, or
  24. > dynamically allocated array.
  25.   ^^^^^^^^^^^
  26.     The reason I don't believe malloc() will work out here is because
  27. sizeof() is a compile-time operation, and therefore cannot be used to
  28. determine the size of a dynamically allocated array.  Everything that
  29. uses sizeof() in C source code will be converted to an unsigned int
  30. (type_t) before linking occurs.  When name is a static array, sizeof()
  31. will correctly give its size.  When it is dynamically allocated,
  32. sizeof() will give the size of a pointer.
  33.  
  34. Example:
  35.  char name[8];
  36.  char *zeffo;
  37.  
  38.  zeffo = (char *)malloc( (12 * sizeof(char)) );
  39.  printf(" array %d\n dynamic %d\n", sizeof(name), sizeof(zeffo));
  40.  free( zeffo );
  41.  
  42. Output (on a PC compiled in the large memory model):
  43.  array 8
  44.  dynamic 4
  45.  
  46.                                           -- Stone
  47. --
  48. ... Is a computer language without GOTO's totally Wirth-less?
  49.  
  50.